import { Basic, InitStorage } from "./index";

/**
 * @description Uniapp Storage的适配器
 */
class UniappStorageAdapter implements Storage {

  get length() {
    const { currentSize } = uni.getStorageInfoSync();
    return currentSize
  };

  clear(): void {
    uni.clearStorageSync()
  }

  removeItem(_Key: string): void {
    uni.removeStorageSync(_Key)
  }

  setItem(_Key: string, _Value: string): void {
    uni.setStorageSync(_Key, _Value)
  }

  getItem(_Key: string): string | null {
    return uni.getStorageSync(_Key);
  }

  key(_Index: number): string | null {
    const { keys } = uni.getStorageInfoSync();
    return keys[_Index]
  }
}

export class StoragePlus extends Basic {
  constructor(_Name?: string | string[]) {
    super(new UniappStorageAdapter())
    InitStorage(_Name)
  }
}